package com.zhsm.fuxi.utils

import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by GuZhC on 2018/11/16.
 * describe:
 */
object TimeUtils {
    val DEFAULT_DATE_FORMAT = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val DATE_FORMAT_DATE = SimpleDateFormat("yyyy-MM-dd")
    val DATE_SIMPLE_DATA = SimpleDateFormat("yy/MM/dd HH:mm")
    val DATE_CHAIN_DATA = SimpleDateFormat("yyyy年MM月dd日 HH:mm")

    /**
     * long time to string
     *
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    @JvmOverloads
    fun getTime(timeInMillis: Long, dateFormat: SimpleDateFormat = DEFAULT_DATE_FORMAT): String {
        return dateFormat.format(Date(timeInMillis))
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    val currentTimeInLong: Long
        get() = System.currentTimeMillis()

    /**
     * get current time in milliseconds, format is [.DEFAULT_DATE_FORMAT]
     *
     * @return
     */
    val currentTimeInString: String
        get() = getTime(currentTimeInLong)

    /**
     * get current time in milliseconds
     *
     * @return
     */
    fun getCurrentTimeInString(dateFormat: SimpleDateFormat): String {
        return getTime(currentTimeInLong, dateFormat)
    }
    fun getDistanceTime(time1: Long, time2: Long): String {
        var hour: Long = 0
        var min: Long = 0
        var sec: Long = 0
        val diff: Long = if (time1 < time2) {
            time2 - time1
        } else {
            time1 - time2
        }

        hour = diff / (60 * 60 * 1000)
        min = diff / (60 * 1000) - hour * 60
        sec = diff / 1000 - hour * 60 * 60 - min * 60
        if (hour != 0L) return hour.toString() + "小时" + min + "分钟"
        if (min != 0L) return min.toString() + "分钟"
        return if (sec != 0L) sec.toString() + "秒" else "0秒"
    }
}

